<%@page import="shop.factory.DAOFactory"%>
<%@page import="shop.vo.ProductBean"%>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="shop.util.IPTimeStamp"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<%
	SmartUpload mySmartUpload = new SmartUpload();
	mySmartUpload.initialize(pageContext);
	mySmartUpload.upload();
	String ext = null;
	ProductBean product = new ProductBean();
	for (int i = 0;i < mySmartUpload.getFiles().getCount();i ++) {
		ext = mySmartUpload.getFiles().getFile(i).getFileExt();
		IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr());
		String fileName = its.getIPTimeRand() + "." + ext;
		String path = "/Index/images/upload/" + fileName;
		mySmartUpload.getFiles().getFile(i).saveAs(path, SmartUpload.SAVE_VIRTUAL);
		if (i == 0) {
			product.setPro_img(path.substring(7));
		}
		if (i == 1) {
			product.setPro_litlleimg(path.substring(7));
		}
	}
	String proName = mySmartUpload.getRequest().getParameter("proName");
	float price = Float.parseFloat(mySmartUpload.getRequest().getParameter("price"));
	int num = Integer.parseInt(mySmartUpload.getRequest().getParameter("num"));
	String description = mySmartUpload.getRequest().getParameter("proDescr");
	String pro_param = mySmartUpload.getRequest().getParameter("pro_param");
	int lei = 0;
	String first = mySmartUpload.getRequest().getParameter("first");
	String second = mySmartUpload.getRequest().getParameter("second");
	if (!second.equals("")) {
		lei = Integer.parseInt(second);
	} else {
		lei = Integer.parseInt(first);
	}
	//out.println(lei);
	product.setPro_name(proName);
	product.setPrice(price);
	product.setNum(num);
	product.setPro_description(description);
	product.setPro_param(pro_param);
	product.setPro_type_id(lei);
	boolean flag = DAOFactory.getIProductDAOInstance().doCreate(product);
	if (flag) {
		out.println("<script>alert('添加成功！');</script>");
		response.setHeader("refresh", "0;url=productAdd.jsp");
	} else {
		out.println("<script>alert('添加失败！');</script>");
		response.setHeader("refresh", "0;url=productAdd.jsp");
	} 
	
	
%>
</body>
</html>